<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li class="active"><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. socket.io">1. socket.io</a></li><li><a href="#t12. socket.io的特点">2. socket.io的特点</a></li><li><a href="#t23. 初步使用">3. 初步使用</a><ul><li><a href="#t33.1 安装部署">3.1 安装部署</a></li><li><a href="#t43.2 启动服务">3.2 启动服务</a></li><li><a href="#t53.3 客户端引用">3.3 客户端引用</a></li><li><a href="#t63.4 发送消息">3.4 发送消息</a></li></ul></li><li><a href="#t74. 深入分析">4. 深入分析</a><ul><li><a href="#t84.1 send方法">4.1 send方法</a></li><li><a href="#t94.2 服务端事件">4.2 服务端事件</a></li><li><a href="#t104.3 客户端事件">4.3 客户端事件</a></li></ul></li><li><a href="#t115. 划分命名空间">5. 划分命名空间</a><ul><li><a href="#t125.1 服务器端划分命名空间">5.1 服务器端划分命名空间</a></li><li><a href="#t135.2 客户端连接命名空间">5.2 客户端连接命名空间</a></li></ul></li><li><a href="#t146. 房间">6. 房间</a><ul><li><a href="#t156.1  进入房间">6.1  进入房间</a></li><li><a href="#t166.2  离开房间">6.2  离开房间</a></li></ul></li><li><a href="#t177. 全局广播">7. 全局广播</a><ul><li><a href="#t187.1 向大厅和所有人房间内的人广播">7.1 向大厅和所有人房间内的人广播</a></li><li><a href="#t197.2 向除了自己外的所有人广播">7.2 向除了自己外的所有人广播</a></li></ul></li><li><a href="#t208. 房间内广播">8. 房间内广播</a><ul><li><a href="#t218.1 向房间内广播">8.1 向房间内广播</a></li><li><a href="#t228.2 向房间内广播">8.2 向房间内广播</a></li></ul></li><li><a href="#t239. 聊天室">9. 聊天室</a></li><li><a href="#t2410. 参考">10. 参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. socket.io">1. socket.io <a href="#t01. socket.io"> # </a></h2>
<p>Socket.IO&#x662F;&#x4E00;&#x4E2A;WebSocket&#x5E93;&#xFF0C;&#x5305;&#x62EC;&#x4E86;&#x5BA2;&#x6237;&#x7AEF;&#x7684;js&#x548C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;nodejs&#xFF0C;&#x5B83;&#x7684;&#x76EE;&#x6807;&#x662F;&#x6784;&#x5EFA;&#x53EF;&#x4EE5;&#x5728;&#x4E0D;&#x540C;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x79FB;&#x52A8;&#x8BBE;&#x5907;&#x4E0A;&#x4F7F;&#x7528;&#x7684;&#x5B9E;&#x65F6;&#x5E94;&#x7528;&#x3002;</p>
<h2 id="t12. socket.io&#x7684;&#x7279;&#x70B9;">2. socket.io&#x7684;&#x7279;&#x70B9; <a href="#t12. socket.io&#x7684;&#x7279;&#x70B9;"> # </a></h2>
<ul>
<li>&#x6613;&#x7528;&#x6027;&#xFF1A;socket.io&#x5C01;&#x88C5;&#x4E86;&#x670D;&#x52A1;&#x7AEF;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x4F7F;&#x7528;&#x8D77;&#x6765;&#x975E;&#x5E38;&#x7B80;&#x5355;&#x65B9;&#x4FBF;&#x3002;</li>
<li>&#x8DE8;&#x5E73;&#x53F0;&#xFF1A;socket.io&#x652F;&#x6301;&#x8DE8;&#x5E73;&#x53F0;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x4F60;&#x6709;&#x4E86;&#x66F4;&#x591A;&#x7684;&#x9009;&#x62E9;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x81EA;&#x5DF1;&#x559C;&#x6B22;&#x7684;&#x5E73;&#x53F0;&#x4E0B;&#x5F00;&#x53D1;&#x5B9E;&#x65F6;&#x5E94;&#x7528;&#x3002;</li>
<li>&#x81EA;&#x9002;&#x5E94;&#xFF1A;&#x5B83;&#x4F1A;&#x81EA;&#x52A8;&#x6839;&#x636E;&#x6D4F;&#x89C8;&#x5668;&#x4ECE;WebSocket&#x3001;AJAX&#x957F;&#x8F6E;&#x8BE2;&#x3001;Iframe&#x6D41;&#x7B49;&#x7B49;&#x5404;&#x79CD;&#x65B9;&#x5F0F;&#x4E2D;&#x9009;&#x62E9;&#x6700;&#x4F73;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#x7F51;&#x7EDC;&#x5B9E;&#x65F6;&#x5E94;&#x7528;&#xFF0C;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x548C;&#x4EBA;&#x6027;&#x5316;&#xFF0C;&#x800C;&#x4E14;&#x652F;&#x6301;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x6700;&#x4F4E;&#x8FBE;IE5.5&#x3002;</li>
</ul>
<h2 id="t23. &#x521D;&#x6B65;&#x4F7F;&#x7528;">3. &#x521D;&#x6B65;&#x4F7F;&#x7528; <a href="#t23. &#x521D;&#x6B65;&#x4F7F;&#x7528;"> # </a></h2>
<h3 id="t33.1 &#x5B89;&#x88C5;&#x90E8;&#x7F72;">3.1 &#x5B89;&#x88C5;&#x90E8;&#x7F72; <a href="#t33.1 &#x5B89;&#x88C5;&#x90E8;&#x7F72;"> # </a></h3>
<p>&#x4F7F;&#x7528;npm&#x5B89;&#x88C5;socket.io</p>
<pre><code class="lang-javascript">$ npm install socket.io
</code></pre>
<h3 id="t43.2 &#x542F;&#x52A8;&#x670D;&#x52A1;">3.2 &#x542F;&#x52A8;&#x670D;&#x52A1; <a href="#t43.2 &#x542F;&#x52A8;&#x670D;&#x52A1;"> # </a></h3>
<p>&#x521B;&#x5EFA; <code>app.js</code> &#x6587;&#x4EF6;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">var</span> app = express();

app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.sendFile(path.resolve(<span class="hljs-string">&apos;index.html&apos;</span>));
});

<span class="hljs-keyword">var</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>).createServer(app);
<span class="hljs-keyword">var</span> io = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;socket.io&apos;</span>)(server);

io.on(<span class="hljs-string">&apos;connection&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&apos;</span>);
    socket.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">msg</span>) </span>{
        <span class="hljs-built_in">console</span>.log(msg);
        socket.send(<span class="hljs-string">&apos;sever:&apos;</span> + msg);
    });
});
server.listen(<span class="hljs-number">80</span>);
</code></pre>
<h3 id="t53.3 &#x5BA2;&#x6237;&#x7AEF;&#x5F15;&#x7528;">3.3 &#x5BA2;&#x6237;&#x7AEF;&#x5F15;&#x7528; <a href="#t53.3 &#x5BA2;&#x6237;&#x7AEF;&#x5F15;&#x7528;"> # </a></h3>
<p>&#x670D;&#x52A1;&#x7AEF;&#x8FD0;&#x884C;&#x540E;&#x4F1A;&#x5728;&#x6839;&#x76EE;&#x5F55;&#x52A8;&#x6001;&#x751F;&#x6210;socket.io&#x7684;&#x5BA2;&#x6237;&#x7AEF;js&#x6587;&#x4EF6; 
&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x56FA;&#x5B9A;&#x8DEF;&#x5F84;<code>/socket.io/socket.io.js</code>&#x6DFB;&#x52A0;&#x5F15;&#x7528;<br>&#x5BA2;&#x6237;&#x7AEF;&#x52A0;&#x8F7D;socket.io&#x6587;&#x4EF6;&#x540E;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x7684;&#x5BF9;&#x8C61;io<br><code>connect</code>&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;<code>url</code>&#x53C2;&#x6570;&#xFF0C;url&#x53EF;&#x4EE5;socket&#x670D;&#x52A1;&#x7684;http&#x5B8C;&#x6574;&#x5730;&#x5740;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;&#xFF0C;&#x5982;&#x679C;&#x7701;&#x7565;&#x5219;&#x8868;&#x793A;&#x9ED8;&#x8BA4;&#x8FDE;&#x63A5;&#x5F53;&#x524D;&#x8DEF;&#x5F84;
&#x521B;&#x5EFA;index.html&#x6587;&#x4EF6;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/socket.io/socket.io.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
 window.onload = function(){
    var socket = io.connect(&apos;/&apos;);
    //&#x76D1;&#x542C;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x4E8B;&#x4EF6;
    socket.on(&apos;connect&apos;,function(){
        console.log(&apos;&#x8FDE;&#x63A5;&#x6210;&#x529F;&apos;);
    });
    //&#x76D1;&#x542C;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&#x4E8B;&#x4EF6;
    socket.on(&apos;disconnect&apos;,function(){
       console.log(&apos;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&apos;);
    });
 };
</code></pre>
<h3 id="t63.4 &#x53D1;&#x9001;&#x6D88;&#x606F;">3.4 &#x53D1;&#x9001;&#x6D88;&#x606F; <a href="#t63.4 &#x53D1;&#x9001;&#x6D88;&#x606F;"> # </a></h3>
<p>&#x6210;&#x529F;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>socket</code>&#x5BF9;&#x8C61;&#x7684;<code>send</code>&#x51FD;&#x6570;&#x6765;&#x4E92;&#x76F8;&#x53D1;&#x9001;&#x6D88;&#x606F;
&#x4FEE;&#x6539;index.html</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> socket = io.connect(<span class="hljs-string">&apos;/&apos;</span>);
socket.on(<span class="hljs-string">&apos;connect&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
   <span class="hljs-comment">//&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x540E;&#x53D1;&#x9001;&#x6D88;&#x606F;&apos;welcome&apos;</span>
   socket.send(<span class="hljs-string">&apos;welcome&apos;</span>);
});
<span class="hljs-comment">//&#x5BA2;&#x6237;&#x7AEF;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x6D88;&#x606F;&#x540E;&#x89E6;&#x53D1;</span>
socket.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">message</span>)</span>{
   <span class="hljs-built_in">console</span>.log(message);
});
</code></pre>
<p>&#x4FEE;&#x6539;app.js</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> io = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;scoket.io&apos;</span>)(server);
io.on(<span class="hljs-string">&apos;connection&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">socket</span>)</span>{
  <span class="hljs-comment">//&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;</span>
  socket.send(<span class="hljs-string">&apos;&#x6B22;&#x8FCE;&#x5149;&#x4E34;&apos;</span>);
  <span class="hljs-comment">//&#x63A5;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x6D88;&#x606F;&#x65F6;&#x89E6;&#x53D1;</span>
  socket.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>)</span>{
      <span class="hljs-built_in">console</span>.log(data);
  });
});
</code></pre>
<h2 id="t74. &#x6DF1;&#x5165;&#x5206;&#x6790;">4. &#x6DF1;&#x5165;&#x5206;&#x6790; <a href="#t74. &#x6DF1;&#x5165;&#x5206;&#x6790;"> # </a></h2>
<h3 id="t84.1 send&#x65B9;&#x6CD5;">4.1 send&#x65B9;&#x6CD5; <a href="#t84.1 send&#x65B9;&#x6CD5;"> # </a></h3>
<p><code>send</code>&#x51FD;&#x6570;&#x53EA;&#x662F;<code>emit</code>&#x7684;&#x5C01;&#x88C5;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">var</span> args = toArray(<span class="hljs-built_in">arguments</span>);
  args.unshift(<span class="hljs-string">&apos;message&apos;</span>);
  <span class="hljs-keyword">this</span>.emit.apply(<span class="hljs-keyword">this</span>, args);
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}
</code></pre>
<p><code>emit</code>&#x51FD;&#x6570;&#x6709;&#x4E24;&#x4E2A;&#x53C2;&#x6570;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x4E8B;&#x4EF6;&#x540D;&#x79F0;,&#x53D1;&#x9001;&#x65B9;&#x53D1;&#x9001;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x7684;&#x4E8B;&#x4EF6;&#x540D;&#x79F0;,&#x63A5;&#x6536;&#x65B9;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5BF9;&#x5E94;&#x7684;&#x4E8B;&#x4EF6;&#x540D;&#x79F0;&#x6765;&#x76D1;&#x542C;&#x63A5;&#x6536;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x8981;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;</li>
</ul>
<h3 id="t94.2 &#x670D;&#x52A1;&#x7AEF;&#x4E8B;&#x4EF6;">4.2 &#x670D;&#x52A1;&#x7AEF;&#x4E8B;&#x4EF6; <a href="#t94.2 &#x670D;&#x52A1;&#x7AEF;&#x4E8B;&#x4EF6;"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x4E8B;&#x4EF6;&#x540D;&#x79F0;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">connection</td>
<td style="text-align:left">&#x5BA2;&#x6237;&#x7AEF;&#x6210;&#x529F;&#x8FDE;&#x63A5;&#x5230;&#x670D;&#x52A1;&#x5668;</td>
</tr>
<tr>
<td style="text-align:left">message</td>
<td style="text-align:left">&#x63A5;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6D88;&#x606F;</td>
</tr>
<tr>
<td style="text-align:left">disconnect</td>
<td style="text-align:left">&#x5BA2;&#x6237;&#x7AEF;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;</td>
</tr>
<tr>
<td style="text-align:left">error</td>
<td style="text-align:left">&#x76D1;&#x542C;&#x9519;&#x8BEF;</td>
</tr>
</tbody>
</table>
<h3 id="t104.3 &#x5BA2;&#x6237;&#x7AEF;&#x4E8B;&#x4EF6;">4.3 &#x5BA2;&#x6237;&#x7AEF;&#x4E8B;&#x4EF6; <a href="#t104.3 &#x5BA2;&#x6237;&#x7AEF;&#x4E8B;&#x4EF6;"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x4E8B;&#x4EF6;&#x540D;&#x79F0;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">connect</td>
<td style="text-align:left">&#x6210;&#x529F;&#x8FDE;&#x63A5;&#x5230;&#x670D;&#x52A1;&#x5668;</td>
</tr>
<tr>
<td style="text-align:left">message</td>
<td style="text-align:left">&#x63A5;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x7684;&#x6D88;&#x606F;</td>
</tr>
<tr>
<td style="text-align:left">disconnect</td>
<td style="text-align:left">&#x5BA2;&#x6237;&#x7AEF;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;</td>
</tr>
<tr>
<td style="text-align:left">error</td>
<td style="text-align:left">&#x76D1;&#x542C;&#x9519;&#x8BEF;</td>
</tr>
</tbody>
</table>
<h2 id="t115. &#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4;">5. &#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="#t115. &#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4;"> # </a></h2>
<h3 id="t125.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4;">5.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="#t125.1 &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5212;&#x5206;&#x547D;&#x540D;&#x7A7A;&#x95F4;"> # </a></h3>
<ul>
<li>&#x53EF;&#x4EE5;&#x628A;&#x670D;&#x52A1;&#x5206;&#x6210;&#x591A;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x9ED8;&#x8BA4;/,&#x4E0D;&#x540C;&#x7A7A;&#x95F4;&#x5185;&#x4E0D;&#x80FD;&#x901A;&#x4FE1;<pre><code class="lang-js">io.on(<span class="hljs-string">&apos;connection&apos;</span>, socket =&gt; {});
io.of(<span class="hljs-string">&apos;/news&apos;</span>).on(<span class="hljs-string">&apos;connection&apos;</span>, socket =&gt; {});
</code></pre>
<h3 id="t135.2 &#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x547D;&#x540D;&#x7A7A;&#x95F4;">5.2 &#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="#t135.2 &#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x547D;&#x540D;&#x7A7A;&#x95F4;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> socket = io(<span class="hljs-string">&apos;http://localhost/&apos;</span>);
<span class="hljs-keyword">let</span> socket = io(<span class="hljs-string">&apos;http://localhost/news&apos;</span>);
</code></pre>
</li>
</ul>
<h2 id="t146. &#x623F;&#x95F4;">6. &#x623F;&#x95F4; <a href="#t146. &#x623F;&#x95F4;"> # </a></h2>
<ul>
<li>&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5206;&#x6210;&#x591A;&#x4E2A;&#x623F;&#x95F4;&#xFF0C;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x8FDB;&#x5165;&#x591A;&#x4E2A;&#x623F;&#x95F4;&#x3002;</li>
<li>&#x5982;&#x679C;&#x5927;&#x5927;&#x5385;&#x91CC;&#x5E7F;&#x64AD; &#xFF0C;&#x90A3;&#x4E48;&#x6240;&#x6709;&#x5728;&#x5927;&#x5385;&#x91CC;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x4EFB;&#x4F55;&#x623F;&#x95F4;&#x5185;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x90FD;&#x80FD;&#x6536;&#x5230;&#x6D88;&#x606F;&#x3002;</li>
<li>&#x6240;&#x6709;&#x5728;&#x623F;&#x95F4;&#x91CC;&#x7684;&#x5E7F;&#x64AD;&#x548C;&#x901A;&#x4FE1;&#x90FD;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x623F;&#x95F4;&#x4EE5;&#x5916;&#x7684;&#x5BA2;&#x6237;&#x7AEF;</li>
</ul>
<h3 id="t156.1  &#x8FDB;&#x5165;&#x623F;&#x95F4;">6.1  &#x8FDB;&#x5165;&#x623F;&#x95F4; <a href="#t156.1  &#x8FDB;&#x5165;&#x623F;&#x95F4;"> # </a></h3>
<pre><code class="lang-javascript">socket.join(<span class="hljs-string">&apos;chat&apos;</span>);<span class="hljs-comment">//&#x8FDB;&#x5165;chat&#x623F;&#x95F4;</span>
</code></pre>
<h3 id="t166.2  &#x79BB;&#x5F00;&#x623F;&#x95F4;">6.2  &#x79BB;&#x5F00;&#x623F;&#x95F4; <a href="#t166.2  &#x79BB;&#x5F00;&#x623F;&#x95F4;"> # </a></h3>
<pre><code class="lang-javascript">socket.leave(<span class="hljs-string">&apos;chat&apos;</span>);<span class="hljs-comment">//&#x79BB;&#x5F00;chat&#x623F;&#x95F4;</span>
</code></pre>
<h2 id="t177. &#x5168;&#x5C40;&#x5E7F;&#x64AD;">7. &#x5168;&#x5C40;&#x5E7F;&#x64AD; <a href="#t177. &#x5168;&#x5C40;&#x5E7F;&#x64AD;"> # </a></h2>
<p>&#x5E7F;&#x64AD;&#x5C31;&#x662F;&#x5411;&#x591A;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x90FD;&#x53D1;&#x9001;&#x6D88;&#x606F;</p>
<h3 id="t187.1 &#x5411;&#x5927;&#x5385;&#x548C;&#x6240;&#x6709;&#x4EBA;&#x623F;&#x95F4;&#x5185;&#x7684;&#x4EBA;&#x5E7F;&#x64AD;">7.1 &#x5411;&#x5927;&#x5385;&#x548C;&#x6240;&#x6709;&#x4EBA;&#x623F;&#x95F4;&#x5185;&#x7684;&#x4EBA;&#x5E7F;&#x64AD; <a href="#t187.1 &#x5411;&#x5927;&#x5385;&#x548C;&#x6240;&#x6709;&#x4EBA;&#x623F;&#x95F4;&#x5185;&#x7684;&#x4EBA;&#x5E7F;&#x64AD;"> # </a></h3>
<pre><code class="lang-javascript">io.emit(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-string">&apos;&#x5168;&#x5C40;&#x5E7F;&#x64AD;&apos;</span>);
</code></pre>
<h3 id="t197.2 &#x5411;&#x9664;&#x4E86;&#x81EA;&#x5DF1;&#x5916;&#x7684;&#x6240;&#x6709;&#x4EBA;&#x5E7F;&#x64AD;">7.2 &#x5411;&#x9664;&#x4E86;&#x81EA;&#x5DF1;&#x5916;&#x7684;&#x6240;&#x6709;&#x4EBA;&#x5E7F;&#x64AD; <a href="#t197.2 &#x5411;&#x9664;&#x4E86;&#x81EA;&#x5DF1;&#x5916;&#x7684;&#x6240;&#x6709;&#x4EBA;&#x5E7F;&#x64AD;"> # </a></h3>
<pre><code class="lang-js">socket.broadcast.emit(<span class="hljs-string">&apos;message&apos;</span>, msg);
socket.broadcast.emit(<span class="hljs-string">&apos;message&apos;</span>, msg);
</code></pre>
<h2 id="t208. &#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;">8. &#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD; <a href="#t208. &#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;"> # </a></h2>
<h3 id="t218.1 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;">8.1 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD; <a href="#t218.1 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;"> # </a></h3>
<p>&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x7684;&#x89D2;&#x5EA6;&#x6765;&#x63D0;&#x4EA4;&#x4E8B;&#x4EF6;,&#x63D0;&#x4EA4;&#x8005;&#x4F1A;&#x5305;&#x542B;&#x5728;&#x5185;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//2. &#x5411;myRoom&#x5E7F;&#x64AD;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x5728;&#x6B64;&#x623F;&#x95F4;&#x5185;&#x9664;&#x4E86;&#x81EA;&#x5DF1;&#x5916;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#x90FD;&#x4F1A;&#x6536;&#x5230;&#x6D88;&#x606F;</span>
io.in(<span class="hljs-string">&apos;myRoom&apos;</span>).emit(<span class="hljs-string">&apos;message&apos;</span>, msg);
io.of(<span class="hljs-string">&apos;/news&apos;</span>).in(<span class="hljs-string">&apos;myRoom&apos;</span>).emit(<span class="hljs-string">&apos;message&apos;</span>,msg);
</code></pre>
<h3 id="t228.2 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;">8.2 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD; <a href="#t228.2 &#x5411;&#x623F;&#x95F4;&#x5185;&#x5E7F;&#x64AD;"> # </a></h3>
<p>&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x89D2;&#x5EA6;&#x6765;&#x63D0;&#x4EA4;&#x4E8B;&#x4EF6;,&#x63D0;&#x4EA4;&#x8005;&#x4F1A;&#x6392;&#x9664;&#x5728;&#x5916;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//2. &#x5411;myRoom&#x5E7F;&#x64AD;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x5728;&#x6B64;&#x623F;&#x95F4;&#x5185;&#x9664;&#x4E86;&#x81EA;&#x5DF1;&#x5916;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#x90FD;&#x4F1A;&#x6536;&#x5230;&#x6D88;&#x606F;</span>
socket.broadcast.to(<span class="hljs-string">&apos;myroom&apos;</span>).emit(<span class="hljs-string">&apos;message&apos;</span>, msg);
socket.broadcast.to(<span class="hljs-string">&apos;myroom&apos;</span>).emit(<span class="hljs-string">&apos;message&apos;</span>, msg);
</code></pre>
<h2 id="t239. &#x804A;&#x5929;&#x5BA4;">9. &#x804A;&#x5929;&#x5BA4; <a href="#t239. &#x804A;&#x5929;&#x5BA4;"> # </a></h2>
<p>app.js</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//express+socket&#x8054;&#x5408;&#x4F7F;&#x7528;</span>
<span class="hljs-comment">//express&#x8D1F;&#x8D23; &#x8FD4;&#x56DE;&#x9875;&#x9762;&#x548C;&#x6837;&#x5F0F;&#x7B49;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;socket.io&#x8D1F;&#x8D23; &#x6D88;&#x606F;&#x901A;&#x4FE1;</span>
<span class="hljs-keyword">let</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> app = express();
app.get(<span class="hljs-string">&apos;/news&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.sendFile(path.resolve(__dirname, <span class="hljs-string">&apos;public/news.html&apos;</span>));
});
app.get(<span class="hljs-string">&apos;/goods&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.sendFile(path.resolve(__dirname, <span class="hljs-string">&apos;public/goods.html&apos;</span>));
});
<span class="hljs-keyword">let</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>).createServer(app);
<span class="hljs-keyword">let</span> io = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;socket.io&apos;</span>)(server);
<span class="hljs-comment">//&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x8FDE;&#x63A5;</span>
<span class="hljs-comment">//&#x547D;&#x540D;&#x662F;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x9694;&#x79BB;&#x7684;</span>
<span class="hljs-keyword">let</span> sockets = {};
io.on(<span class="hljs-string">&apos;connection&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-comment">//&#x5F53;&#x524D;&#x7528;&#x6237;&#x6240;&#x6709;&#x7684;&#x623F;&#x95F4;</span>
    <span class="hljs-keyword">let</span> rooms = [];
    <span class="hljs-keyword">let</span> username;<span class="hljs-comment">//&#x7528;&#x6237;&#x540D;&#x521A;&#x5F00;&#x59CB;&#x7684;&#x65F6;&#x5019;&#x662F;undefined</span>
    <span class="hljs-comment">//&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x6D88;&#x606F;</span>
    socket.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">message</span>) </span>{
        <span class="hljs-keyword">if</span> (username) {
            <span class="hljs-comment">//&#x5982;&#x679C;&#x8BF4;&#x5728;&#x67D0;&#x4E2A;&#x623F;&#x95F4;&#x5185;&#x7684;&#x8BDD;&#x90A3;&#x4E48;&#x4ED6;&#x8BF4;&#x7684;&#x8BDD;&#x53EA;&#x4F1A;&#x8BF4;&#x7ED9;&#x623F;&#x95F4;&#x5185;&#x7684;&#x4EBA;&#x542C;</span>
            <span class="hljs-keyword">if</span> (rooms.length &gt; <span class="hljs-number">0</span>) {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; rooms.length; i++) {
                    <span class="hljs-comment">//&#x5728;&#x6B64;&#x5904;&#x8981;&#x5224;&#x65AD;&#x662F;&#x79C1;&#x804A;&#x8FD8;&#x662F;&#x516C;&#x804A;</span>
                    <span class="hljs-keyword">let</span> result = message.match(<span class="hljs-regexp">/@([^ ]+) (.+)/</span>);
                    <span class="hljs-keyword">if</span> (result) {
                        <span class="hljs-keyword">let</span> toUser = result[<span class="hljs-number">1</span>];
                        <span class="hljs-keyword">let</span> content = result[<span class="hljs-number">2</span>];
                        sockets[toUser].send({
                            username,
                            content,
                            <span class="hljs-attr">createAt</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
                        });
                    } <span class="hljs-keyword">else</span> {
                        io.in(rooms[i]).emit(<span class="hljs-string">&apos;message&apos;</span>, {
                            username,
                            <span class="hljs-attr">content</span>: message,
                            <span class="hljs-attr">createAt</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
                        });
                    }

                }
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-comment">//&#x5982;&#x679C;&#x6B64;&#x7528;&#x6237;&#x4E0D;&#x5728;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x623F;&#x95F4;&#x5185;&#x7684;&#x8BDD;&#x9700;&#x8981;&#x5168;&#x5C40;&#x5E7F;&#x64AD; </span>
                <span class="hljs-keyword">let</span> result = message.match(<span class="hljs-regexp">/@([^ ]+) (.+)/</span>);
                <span class="hljs-keyword">if</span> (result) {
                    <span class="hljs-keyword">let</span> toUser = result[<span class="hljs-number">1</span>];
                    <span class="hljs-keyword">let</span> content = result[<span class="hljs-number">2</span>];
                    sockets[toUser].send({
                        username,
                        content,
                        <span class="hljs-attr">createAt</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
                    });
                } <span class="hljs-keyword">else</span> {
                    io.emit(<span class="hljs-string">&apos;message&apos;</span>, {
                        username,
                        <span class="hljs-attr">content</span>: message,
                        <span class="hljs-attr">createAt</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
                    });
                }
            }
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">//&#x5982;&#x679C;&#x7528;&#x6237;&#x540D;&#x8FD8;&#x6CA1;&#x6709;&#x8BBE;&#x7F6E;&#x8FC7;&#xFF0C;&#x90A3;&#x8BF4;&#x660E;&#x8FD9;&#x662F;&#x8FD9;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x7B2C;&#x4E00;&#x6B21;&#x53D1;&#x8A00;</span>
            username = message;
            <span class="hljs-comment">//&#x5728;&#x5BF9;&#x8C61;&#x4E2D;&#x7F13;&#x5B58; key&#x662F;&#x7528;&#x6237;&#x540D; &#x503C;&#x662F;socket</span>
            sockets[username] = socket;
            socket.broadcast.emit(<span class="hljs-string">&apos;message&apos;</span>, {
                <span class="hljs-attr">username</span>: <span class="hljs-string">&apos;&#x7CFB;&#x7EDF;&apos;</span>,
                <span class="hljs-attr">content</span>: <span class="hljs-string">`&lt;a&gt;<span class="hljs-subst">${username}</span>&lt;/a&gt; &#x52A0;&#x5165;&#x4E86;&#x804A;&#x5929;`</span>,
                <span class="hljs-attr">createAt</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
            });
        }


    });
    <span class="hljs-comment">//&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;join&#x7C7B;&#x578B;&#x7684;&#x6D88;&#x606F;,&#x53C2;&#x6570;&#x662F;&#x8981;&#x52A0;&#x5165;&#x7684;&#x623F;&#x95F4;&#x540D;</span>
    socket.on(<span class="hljs-string">&apos;join&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">roomName</span>) </span>{
        <span class="hljs-keyword">let</span> oldIndex = rooms.indexOf(roomName);
        <span class="hljs-keyword">if</span> (oldIndex == <span class="hljs-number">-1</span>) {
            socket.join(roomName);<span class="hljs-comment">//&#x76F8;&#x5F53;&#x4E8E;&#x8FD9;&#x4E2A;socket&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8FDB;&#x5165;&#x4E86;&#x67D0;&#x4E2A;&#x623F;&#x95F4; </span>
            rooms.push(roomName);
        }
    })
    <span class="hljs-comment">//&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x544A;&#x8BC9;&#x670D;&#x52A1;&#x5668;&#x8BF4;&#x8981;&#x79BB;&#x5F00;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5219;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x5C31;&#x5728;&#x623F;&#x95F4;&#x5185;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x79BB;&#x5F00;&#x8FD9;&#x4E2A;&#x623F;&#x95F4;</span>
    socket.on(<span class="hljs-string">&apos;leave&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">roomName</span>) </span>{
        <span class="hljs-keyword">let</span> oldIndex = rooms.indexOf(roomName);
        <span class="hljs-keyword">if</span> (oldIndex != <span class="hljs-number">-1</span>) {
            socket.leave(roomName);
            rooms.splice(oldIndex, <span class="hljs-number">1</span>);
        }
    });
    socket.on(<span class="hljs-string">&apos;getRoomInfo&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(io);
        <span class="hljs-comment">//let rooms = io.manager.rooms;</span>
        <span class="hljs-built_in">console</span>.log(io);
    });
});
<span class="hljs-comment">// io.of(&apos;/goods&apos;).on(&apos;connection&apos;, function (socket) {</span>
<span class="hljs-comment">//     //&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x6D88;&#x606F;</span>
<span class="hljs-comment">//     socket.on(&apos;message&apos;, function (message) {</span>
<span class="hljs-comment">//         socket.send(&apos;goods:&apos; + message);</span>
<span class="hljs-comment">//     });</span>
<span class="hljs-comment">// });</span>

server.listen(<span class="hljs-number">8080</span>);
<span class="hljs-comment">/**
 * 1. &#x53EF;&#x4EE5;&#x628A;&#x670D;&#x52A1;&#x5206;&#x6210;&#x591A;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x9ED8;&#x8BA4;/,&#x4E0D;&#x540C;&#x7A7A;&#x95F4;&#x5185;&#x4E0D;&#x80FD;&#x901A;&#x4FE1;
 * 2. &#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5206;&#x6210;&#x591A;&#x4E2A;&#x623F;&#x95F4;&#xFF0C;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x8FDB;&#x5165;&#x591A;&#x4E2A;&#x623F;&#x95F4;&#x3002;
 * 3. &#x5982;&#x679C;&#x5927;&#x5927;&#x5385;&#x91CC;&#x5E7F;&#x64AD; &#xFF0C;&#x90A3;&#x4E48;&#x6240;&#x6709;&#x5728;&#x5927;&#x5385;&#x91CC;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x4EFB;&#x4F55;&#x623F;&#x95F4;&#x5185;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x90FD;&#x80FD;&#x6536;&#x5230;&#x6D88;&#x606F;&#x3002;
 */</span>
</code></pre>
<p>index.html</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;ie=edge&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.bootcss.com/bootstrap/3.3.1/css/bootstrap.css&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-class">.user</span> {
            <span class="hljs-attribute">color</span>: green;
            <span class="hljs-attribute">cursor</span>: pointer;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&#x804A;&#x5929;&#x5BA4;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-8 col-md-offset-2&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel panel-default&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-heading text-center&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;join(&apos;red&apos;)&quot;</span>&gt;</span>&#x8FDB;&#x5165;&#x7EA2;&#x623F;&#x95F4;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;leave(&apos;red&apos;)&quot;</span>&gt;</span>&#x79BB;&#x5F00;&#x7EA2;&#x623F;&#x95F4;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;join(&apos;green&apos;)&quot;</span>&gt;</span>&#x8FDB;&#x5165;&#x7EFF;&#x623F;&#x95F4;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;leave(&apos;green&apos;)&quot;</span>&gt;</span>&#x8FDB;&#x5165;&#x7EFF;&#x623F;&#x95F4;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;getRoomInfo()&quot;</span>&gt;</span>
                                &#x83B7;&#x53D6;&#x623F;&#x95F4;&#x4FE1;&#x606F;
                            <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;messages&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;clickUser(event)&quot;</span>&gt;</span>

                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-footer&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-10&quot;</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;textMsg&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-2&quot;</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;button&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;send()&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span>&gt;</span>&#x53D1;&#x8A00;<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>


    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/socket.io/socket.io.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">let</span> socket = io(<span class="hljs-string">&apos;/&apos;</span>);
        <span class="hljs-keyword">let</span> textMsg = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#textMsg&apos;</span>);
        <span class="hljs-keyword">let</span> messagesEle = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#messages&apos;</span>);
        socket.on(<span class="hljs-string">&apos;connect&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x6210;&#x529F;&apos;</span>);
        });
        socket.on(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">messageObj</span>) </span>{
            <span class="hljs-keyword">let</span> li = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;li&apos;</span>);
            li.innerHTML = <span class="hljs-string">`&lt;span class=&quot;user&quot;&gt;<span class="hljs-subst">${messageObj.username}</span>&lt;/span&gt;:<span class="hljs-subst">${messageObj.content}</span> &lt;span class=&quot;text-right&quot;&gt;<span class="hljs-subst">${messageObj.createAt.toLocaleString()}</span>&lt;/span&gt;`</span>;
            li.className = <span class="hljs-string">&apos;list-group-item&apos;</span>;
            messagesEle.appendChild(li);
        });

        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">send</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">let</span> content = textMsg.value;
            <span class="hljs-keyword">if</span> (!content)
                <span class="hljs-keyword">return</span> alert(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&#x804A;&#x5929;&#x5185;&#x5BB9;&apos;</span>);
            socket.send(content);
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">join</span>(<span class="hljs-params">name</span>) </span>{
            <span class="hljs-comment">//&#x5411;&#x540E;&#x53F0;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#xFF0C;join name&#x662F;&#x623F;&#x95F4;&#x540D;</span>
            socket.emit(<span class="hljs-string">&apos;join2&apos;</span>, name);
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">leave</span>(<span class="hljs-params">name</span>) </span>{
            <span class="hljs-comment">//&#x5411;&#x540E;&#x53F0;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#xFF0C;&#x79BB;&#x5F00;&#x67D0;&#x4E2A;&#x623F;&#x95F4;</span>
            socket.emit(<span class="hljs-string">&apos;leave3&apos;</span>, name);
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getRoomInfo</span>(<span class="hljs-params"></span>) </span>{
            socket.emit(<span class="hljs-string">&apos;getRoomInfo&apos;</span>);
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">clickUser</span>(<span class="hljs-params">event</span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;clickUser&apos;</span>, event.target.className);
            <span class="hljs-keyword">if</span> (event.target.className == <span class="hljs-string">&apos;user&apos;</span>) {
                <span class="hljs-keyword">let</span> username = event.target.innerHTML;
                textMsg.value = <span class="hljs-string">`@<span class="hljs-subst">${username}</span> `</span>;
            }
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h2 id="t2410. &#x53C2;&#x8003;">10. &#x53C2;&#x8003; <a href="#t2410. &#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="http://socket.io">socket.io</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
